//
//  LKElearnViewController.m
//  YLHelper
//
//  Created by she1323 on 2018/5/8.
//  Copyright © 2018年 she1323. All rights reserved.
//


#import "LKElearnViewController.h"
#import "LKElearnCollectionViewCell.h"
#import "LKSearchBar.h"
#import "LKElearningAvPlayerViewController.h"
#import "LKElearningTestViewController.h"
#import "LKElearningTestpageBankViewController.h"
#import "LKMyViewController.h"
#import "LKVideoModel.h"
@interface LKElearnViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UISearchBarDelegate>

@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)UICollectionViewFlowLayout *flowLayout;
@property(nonatomic,strong)UIButton *myButton;
@property(nonatomic,strong)NSMutableArray *videolistArray;

@property(nonatomic,strong)UIButton *kasoBtn;
@end

@implementation LKElearnViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getVideoListWithVideoName:@""];
    [self setcollectionView];
    self.myButton.titleLabel.font = [UIFont systemFontOfSize:11];
    [self.myButton addTarget:self action:@selector(gogogo) forControlEvents:UIControlEventTouchUpInside];
    // Do any additional setup after loading the view.
}
-(void)gogogo{
    LKMyViewController *vc = [[LKMyViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)setNavgationBackBtn{
    [super setNavgationBackBtn];
    WS(weakSelf)
    self.navigationItem.title = @"E-Learning";
    self.kasoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.kasoBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.kasoBtn setTitle:@"考试" forState:UIControlStateNormal];
    [self.kasoBtn setTitleColor:blueDefault forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.kasoBtn];
    [[self.kasoBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        LKElearningTestpageBankViewController *vc = [[LKElearningTestpageBankViewController alloc] init];
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    [self judegeWheatherContainKaoshi];
}

/**
 判断是否有考试试卷
 */
-(void)judegeWheatherContainKaoshi{
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:judgeWheatherKaoshiUrl header:[LKUserInforTool header] parameters:nil progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        if ([responseObject[@"result"] boolValue]) {
            UIView *redpointV = [[UIView alloc] init];
            redpointV.backgroundColor = [UIColor redColor];
            [self.kasoBtn addSubview:redpointV];
            [redpointV mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.kasoBtn.mas_top);
                make.right.equalTo(self.kasoBtn.mas_right).offset(5);
                make.size.mas_equalTo(CGSizeMake(8, 8));
            }];
            redpointV.layer.cornerRadius = 4;
            redpointV.layer.masksToBounds = YES;
        }else{
            
        }

    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}
/**
 设置collectionview
 */
-(void)setcollectionView{
    UIView * topView = [[UIView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, WIDTH, 46)];
    topView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topView];
    //创建searchBar
    LKSearchBar *searchBar = [[LKSearchBar alloc] initWithFrame:CGRectMake(8, 8, WIDTH - 16, 30)];
    //默认提示文字
    searchBar.placeholder = @"输入视频名称查询";
    searchBar.delegate = self;
    [topView addSubview:searchBar];
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.itemSize = CGSizeMake((WIDTH - 45)/2, 175);
    self.flowLayout.minimumLineSpacing = 12;
    self.flowLayout.minimumInteritemSpacing = 9;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT + 58 , WIDTH, HEIGHT - NAVIGATION_BAR_HEIGHT - 58) collectionViewLayout:self.flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"LKElearnCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"LKElearnCollectionViewCell"];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.videolistArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LKElearnCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LKElearnCollectionViewCell" forIndexPath:indexPath];
    cell.model = self.videolistArray[indexPath.row];
    return cell;
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(20, 16, 0, 16);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    WS(weakSelf)
    CATransition *animation = [CATransition animation];
    [animation setDuration:0.3];
    [animation setType:kCATransitionPush];
    [animation setSubtype:kCATransitionFromRight];
    [[[[UIApplication sharedApplication] keyWindow] layer] addAnimation:animation forKey:nil];
    LKElearningAvPlayerViewController *vc = [[LKElearningAvPlayerViewController alloc] init];
    LKVideoModel *videoModel = self.videolistArray[indexPath.row];
    vc.videoModel = videoModel;
    vc.collectNumber = videoModel.collectionCount;
    [vc.pushSubject subscribeNext:^(id  _Nullable x) {
        LKElearningTestViewController *vc = [[LKElearningTestViewController alloc] init];
        vc.paperId = x;
        [weakSelf.navigationController pushViewController:vc animated:YES];

    }];
    [self presentViewController:vc animated:NO completion:nil];
//    [self.navigationController pushViewController:vc animated:YES];
}
-(UIButton *)myButton{
    if (!_myButton) {
        _myButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_myButton setTitle:@"我的记录" forState:UIControlStateNormal];
        [_myButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        _myButton.backgroundColor = blueDefault;
        [self.view addSubview:_myButton];
        [_myButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_myButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.view.mas_right).mas_offset(-10);
            make.bottom.mas_equalTo(self.view).mas_offset(-80 - TAB_BAR_HEIGHT);
            make.size.mas_equalTo(CGSizeMake(60, 60));
        }];
        _myButton.layer.cornerRadius = 30;
        _myButton.layer.masksToBounds = YES;
        [self.view bringSubviewToFront:_myButton];
    }
    return _myButton;
}

/**
 获取视频列表
 */
-(void)getVideoListWithVideoName:(NSString *)videoName{
    [self.videolistArray removeAllObjects];
    NSDictionary *param = @{@"VideoName":videoName};
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:VideoListUrl header:[LKUserInforTool header] parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        NSArray *videosArray = responseObject[@"result"];
        for (NSDictionary *dic in videosArray) {
            LKVideoModel *model = [LKVideoModel modelWithDictionary:dic];
            [self.videolistArray addObject:model];
        }
        
        [self.collectionView reloadData];
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}
#pragma mark - UISearchBarDelegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    return YES;
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    searchBar.showsCancelButton = YES;
    
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"SearchButton");
    NSLog(@"%@",searchBar.text);
    [self getVideoListWithVideoName:searchBar.text];
    [searchBar resignFirstResponder];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    //    [searchBar setShowsCancelButton:NO animated:YES];
    searchBar.showsCancelButton = NO;
    searchBar.text = @"";
    [self getVideoListWithVideoName:searchBar.text];
    [searchBar resignFirstResponder];
    
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    //       [searchBar resignFirstResponder];
}

-(NSMutableArray *)videolistArray{
    if (!_videolistArray) {
        _videolistArray = [[NSMutableArray alloc] init];
    }
    return _videolistArray;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
